﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OBDUDSUtils.CNNMode;

namespace OBDUDSUtils.Common
{
    public class MessageManager
    {
        public delegate void LogMessageDel(string message);

        public static event LogMessageDel LogMessageEvent;

        private static object fileObj = new object();

        /// <summary>
        /// 更新log
        /// </summary>
        /// <param name="message"></param>
        public static void LogMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var _date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            message = _date + "  " + message;
            LogMessageEvent(message);
        }

        public static void SaveTestData(string data, string file)
        {
            lock (fileObj)
            {
                using (FileStream fs = new FileStream(file, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(data);
                        sw.Flush();
                    }
                }
            }
        }

        public static void SaveOBDModeData(DiagBasic.ModelSID modeType, List<DiagEntity> data, string file)
        {
            var message = GetModeHeaderMessage(modeType);
            var result = GetSidData(modeType, data);

            SaveTestData(message + result.ToString() + "--------------------------------------------------------\r\n\r\n", file);
        }

        public static void AddSidData(DiagBasic.ModelSID modeType, List<DiagEntity> data, ref DiagData diag)
        {
            var message = GetModeHeaderMessage(modeType);
            diag.AllSaveSidData.AppendLine(message);
            diag.AllSaveSidData.AppendLine(GetSidData(modeType, data).ToString());
            diag.AllSaveSidData.AppendLine("--------------------------------------------------------\r\n");
        }

        private static StringBuilder GetSidData(DiagBasic.ModelSID modeType, List<DiagEntity> data)
        {
            StringBuilder result = new StringBuilder();
            foreach (var item in data)
            {
                switch (modeType)
                {
                    case DiagBasic.ModelSID.SID_01:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid12.PIDClumnStr, 15, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid12.TestItemValue, 20, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid12.TestItemUnit, 10, out resultStr);
                            result.AppendLine(resultStr);

                            int size = item.OBDDataSid12.TestItemDescrible.Length + item.OBDDataSid12.TestResult.Length;
                            ConvertSidData(item.OBDDataSid12.TestItemDescrible + item.OBDDataSid12.TestResult, size + 5, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                    case DiagBasic.ModelSID.SID_02:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid12.PIDClumnStr, 15, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid12.TestItemValue, 20, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid12.TestItemUnit, 10, out resultStr);
                            result.AppendLine(resultStr);

                            int size = item.OBDDataSid12.TestItemDescrible.Length + item.OBDDataSid12.TestResult.Length;
                            ConvertSidData(item.OBDDataSid12.TestItemDescrible + item.OBDDataSid12.TestResult, size + 5, out resultStr);
                            result.AppendLine(resultStr);

                            break;
                        }
                    case DiagBasic.ModelSID.SID_03:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid37A.FaultCode, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid37A.Describle, item.OBDDataSid37A.Describle.Length, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                    case DiagBasic.ModelSID.SID_04:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid4.ClearResult, item.OBDDataSid4.ClearResult.Length, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                    case DiagBasic.ModelSID.SID_06:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid6Data.MonitorID, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.TestID, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.UnitID, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.TestValue, 15, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.TestMinVaule, 15, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.TestMaxValue, 15, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.UnitStr, 8, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid6Data.Describle, item.OBDDataSid6Data.Describle.Length, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                    case DiagBasic.ModelSID.SID_07:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid37A.FaultCode, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid37A.Describle, item.OBDDataSid37A.Describle.Length, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                    case DiagBasic.ModelSID.SID_09:
                        {
                            string resultStr;
                            ConvertSidData(item.OBDDataSid9.PIDClumnStr, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid9.TestItemValue, 10, out resultStr);
                            result.AppendLine(resultStr);

                            ConvertSidData(item.OBDDataSid9.TestItemDescrible, item.OBDDataSid9.TestItemDescrible.Length, out resultStr);
                            result.AppendLine(resultStr);
                            break;
                        }
                }
                //result.AppendLine(item.ToString().Replace("|", "     "));
            }
            return result;
        }

        private static void ConvertSidData(string content, int size, out string resultStr)
        {
            var data = System.Text.Encoding.Default.GetBytes(content);
            byte[] buffer = new byte[size];
            if (data.Length <= buffer.Length)
            {
                Array.Copy(data, 0, buffer, 0, data.Length);
                resultStr = System.Text.Encoding.Default.GetString(buffer);
            }
            else//定义的长度溢出
            {
                ConvertSidData(content, size + 2, out resultStr);
            }
        }

        public static void SaveAllOBDModeData(StringBuilder buffer, string file)
        {
            SaveTestData(buffer.ToString() + "--------------------------------------------------------\r\n\r\n", file);
        }

        public static string GetModeHeaderMessage(DiagBasic.ModelSID modeType)
        {
            var message = "日期：" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
            switch (modeType)
            {
                case DiagBasic.ModelSID.SID_01:
                    {
                        message += "Mode 1 - 当前动力总成数据\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_02:
                    {
                        message += "Mode 2 - 当前冻结帧数据\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_03:
                    {
                        message += "Mode 3 - 已确认的排放相关的故障码\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_04:
                    {
                        message += "Mode 4 - 清除故障码\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_06:
                    {
                        message += "Mode 6 - 针对特定被监控系统的车载监控测试结果\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_07:
                    {
                        message += "Mode 7 - 当前或上个驾驶循环排放相关的故障代码\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_09:
                    {
                        message += "Mode 9 - 车辆信息\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                case DiagBasic.ModelSID.SID_0A:
                    {
                        message += "Mode A - 排放相关的永久性故障代码\r\n--------------------------------------------------------\r\n\r\n";
                        break;
                    }
                default:
                    break;
            }
            return message;
        }

        public static void SaveAutoTestData(string data, string directory)
        {
            #region 数据格式与内容
            /*
             * 1)保存测试的故障码及其相关故障信息
             * 2）保存当前故障码汽车条件
             * 3）保存注入故障状态
             * 4）查询到故障码时：
             *      1）保存查询到的汽车条件值
             *      2）保存查询到的故障码
             *      3）保存清除故障码状态
             *      3）当前故障码结束
             * 开始下一个故障码
             */
            #endregion

            //SaveTestData(data, CANBasic.GetAutoTestSavePath(directory));
        }
    }
}
